package com.gcph.zhuoba.main.test;

import java.io.FileDescriptor;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;

import com.gcph.zhuoba.R;


public class MediaPlayerActivity extends Activity implements MediaPlayer.OnCompletionListener,MediaPlayer.OnErrorListener,
        MediaPlayer.OnInfoListener,MediaPlayer.OnPreparedListener,MediaPlayer.OnSeekCompleteListener,
        MediaPlayer.OnVideoSizeChangedListener,SurfaceHolder.Callback,MediaController.MediaPlayerControl,
        GestureDetector.OnGestureListener{


    private SurfaceView _surfaceView;
    private SurfaceHolder _surfaceHolder;
    private MediaController _mediaController;
    private MediaPlayer _mediaPlayer;
    private GestureDetector _gestureDetector;
    private int _position = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_media_player);
        _surfaceView = (SurfaceView)this.findViewById(R.id.surface_view);
        _surfaceHolder = _surfaceView.getHolder();
        _surfaceHolder.addCallback(this);
        _surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        _mediaPlayer = new MediaPlayer();
        _mediaPlayer.setOnCompletionListener(this);
        _mediaPlayer.setOnErrorListener(this);
        _mediaPlayer.setOnInfoListener(this);
        _mediaPlayer.setOnPreparedListener(this);
        _mediaPlayer.setOnSeekCompleteListener(this);
        _mediaPlayer.setOnVideoSizeChangedListener(this);



        _mediaController = new MediaController(this);
        _gestureDetector = new GestureDetector(this,this);



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return _gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        if(_mediaController.isShowing()){
            _mediaController.hide();
        }else{
            _mediaController.show();
        }
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
       // getMenuInflater().inflate(R.menu.media_player, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Log.i("surface_created","surface_created");

        AssetManager assetManager =  this.getAssets();

        Toast toast = Toast.makeText(this,"surface view 生成 position= "+String.valueOf(_position),Toast.LENGTH_SHORT);
        toast.show();

        try {
            _mediaPlayer.reset();

            AssetFileDescriptor assetFileDescriptor = assetManager.openFd("test2.mp4");
            FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();
            _mediaPlayer.setDataSource(fileDescriptor,assetFileDescriptor.getStartOffset(),assetFileDescriptor.getLength());
            //_mediaPlayer.setDataSource(this,Uri.parse("http://192.168.0.185:8080/sotest/files/default/2.2.mp4"));
            _mediaPlayer.setDisplay(_surfaceHolder);

            _mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i("surface_changed","surface_changed");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Toast toast = Toast.makeText(this,"surface view 销毁",Toast.LENGTH_SHORT);
        toast.show();
        Log.i("surface_destroy","surface_destroy");
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.i("video_completion","video_completion");
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {

        return false;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {

        if(what == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING){
            Log.i("media_info_Message","MEDIA_INFO_BAD_INTERLEAVING");
        }

        if(what == MediaPlayer.MEDIA_INFO_BUFFERING_END){
            Log.i("media_info_Message","MEDIA_INFO_BUFFERING_END");
        }

        if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START){
            Log.i("media_info_Message","3");
        }

        if(what == MediaPlayer.MEDIA_INFO_METADATA_UPDATE){
            Log.i("media_info_Message","MEDIA_INFO_METADATA_UPDATE");
        }

        if(what == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE){
            Log.i("media_info_Message","MEDIA_INFO_NOT_SEEKABLE");
        }

        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {

        Log.i("media_prepared","media_prepared");
        if(_position > 0){
            _mediaPlayer.seekTo(_position);
            _position = 0;
        }
        _mediaController.setMediaPlayer(this);
        _mediaController.setAnchorView(findViewById(R.id.media_player_main_view));
        _mediaController.setEnabled(true);
        _mediaController.show();
        _mediaPlayer.start();
        _mediaPlayer.pause();
    }

    @Override
    public void onSeekComplete(MediaPlayer mp) {
        Toast toast = Toast.makeText(this,"seek complete ",Toast.LENGTH_SHORT);
        toast.show();
        Log.i("seek_complete","seek_complete");
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

    }

    @Override
    public void start() {
        _mediaPlayer.start();
    }

    @Override
    public void pause() {
        if(_mediaPlayer.isPlaying())
            _mediaPlayer.pause();
    }

    @Override
    public int getDuration() {
        return _mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        return _mediaPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(int pos) {
        _mediaPlayer.seekTo(pos);
    }

    @Override
    public boolean isPlaying() {
        return _mediaPlayer.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    protected void onPause(){

        _position = _mediaPlayer.getCurrentPosition();

        if(_mediaPlayer.isPlaying()){
            _mediaPlayer.stop();
        }
        Toast toast = Toast.makeText(this,"视频界面pause（）position="+String.valueOf(_position),Toast.LENGTH_SHORT);
        toast.show();
        super.onPause();
    }

    @Override
    protected void onDestroy(){
        Toast toast = Toast.makeText(this,"视频界面destroy（）",Toast.LENGTH_SHORT);
        toast.show();
        if(_mediaPlayer.isPlaying())
                _mediaPlayer.stop();
        _mediaPlayer.release();
        super.onDestroy();

    }

    @Override
    protected  void onResume(){

        super.onResume();
    }

	public int getAudioSessionId() {
		// TODO Auto-generated method stub
		return 0;
	}

}